﻿/**

 * Copyright (c) 2015-2016, FastDev 刘强 (fastdev@163.com) & Quincy.

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Resources;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.Security.Policy;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Web.Compilation;
using System.Web.Configuration;
using System.Web.Hosting;
using System.Web.Management;
using System.Web.Security;
using System.Web.UI;
using System.Web.Util;
using System.Xml;
using Microsoft.Win32;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Concurrent;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using OF.DB.DataAccess;
using Newtonsoft.Json;
using OF.DB.Test.DataAccess;
using OF.DB.Test.Entity;

namespace OF.DB.Test
{
    public static class TestRunGenerateClass
    {
        public static void TestRun()
        {
            Console.WriteLine("---------test User");
            int uid = TestInsertUser();
            TestUpdateUser(uid);
            

            Console.WriteLine("---------test blog");
            int bid = TestInsertBlog(uid);
            TestUpdateBlog(bid);
            TestDeleteBlog(bid);

            TestDeleteUser(uid);
            Console.ReadLine();
        }
        
        private static int TestInsertBlog(int authid)
        {
            int id = BlogDao.Add(new Blog
            {
                authoruid = authid,
                title = "title1",
                body = "content1",
                documentType = 1,
                pubDate = DateTime.Now,
                editDate = DateTime.Now
            });
            Console.WriteLine("insert result:" + Program.GetJsonString(BlogDao.GetByKey(id)));
            return id;
            //TestUpdate(id);
        }

        private static void TestUpdateBlog(int id)
        {
            var blog = BlogDao.GetByKey(id);
            blog.title = blog.title + "changed";
            blog.editDate = DateTime.Now;
            BlogDao.Update(blog);
            Console.WriteLine("update result:" + Program.GetJsonString(BlogDao.GetByKey(id)));
        }

        private static void TestDeleteBlog(int id)
        {
            BlogDao.DeleteByKey(id);
            var blog = BlogDao.GetByKey(id);
            Console.WriteLine("delete result:" + ((blog == null) ? "" : Program.GetJsonString(blog))); 
        }


        private static int TestInsertUser()
        {
            int id = UserDao.Add(new User
            {
                 account = "a1",
                  jointime = DateTime.Now,
                   name = "n1",
                  pwd = new byte[] {1}
            });
            Console.WriteLine("insert result:" + Program.GetJsonString(UserDao.GetByKey(id)));
            return id;
            //TestUpdate(id);
        }

        private static void TestUpdateUser(int id)
        {
            var User = UserDao.GetByKey(id);
            User.name = User.name + " changed";
            UserDao.Update(User);
            Console.WriteLine("update result:" + Program.GetJsonString(UserDao.GetByKey(id)));
        }

        private static void TestDeleteUser(int id)
        {
            UserDao.DeleteByKey(id);
            var User = UserDao.GetByKey(id);
            Console.WriteLine("delete result:" + ((User == null) ? "" : Program.GetJsonString(User)));
        }
    }
}
